<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item label="职务" prop="dutiesId">
        <el-select v-model="pram.dutiesId" placeholder="职务" clearable filterable default-first-option @blur="onTypeBlur($event)" style="width: 100%">
          <el-option
            v-for="(item,index) in positionList.list"
            :key="index"
            :label="item.name + ' ' + item.duties"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="地点" prop="location">
        <el-input v-model="pram.location" maxlength="255" placeholder="地点" />
      </el-form-item>
      <el-form-item label="工作汇报" prop="report">
        <el-input v-model="pram.report" type="textarea" :rows="10" placeholder="工作汇报"/>
      </el-form-item>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="开始日期" prop="startTime">
            <el-date-picker v-model="pram.startTime" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" placement="bottom-end" @change="onchangeTime" placeholder="开始日期"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间" prop="time">
            <el-select v-model="pram.time" placeholder="开始时间" clearable style="width: 100%">
              <el-option label="上午" :value="1"/>
              <el-option label="下午" :value="2"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
<!--      <el-form-item label="排序">-->
<!--        <el-input-number v-model="pram.sort" :min="0"/>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:supervise:report:update','admin:supervise:report:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as reportApi from '@/api/reportApi.js'
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allPositionList:{
      type: Object
    }
  },
  data() {
    return {
      constants:this.$constants,
      pram: {
        dutiesId: null,
        location: null,
        report:null,
        startTime:null,
        time:null,
        //sort: 0,
        id: 0,
      },
      rules: {
        dutiesId: [{ required: true, message: '请选择职务', trigger: ['blur', 'change'] }],
        location: [{ required: true, message: '请填地点', trigger: ['blur', 'change'] }],
        report: [{ required: true, message: '请填工作汇报', trigger: ['blur'] }],
        startTime: [{ required: true, message: '请选择开始日期', trigger: ['blur', 'change'] }],
        time: [{ required: true, message: '请选择开始时间', trigger: ['blur', 'change'] }],
      },

      positionList:{},
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    onchangeTime (e) {
      this.pram.startTime = e;
    },
    onTypeBlur(e) {
      if (e.target.value) {
        this.pram.dutiesId = e.target.value;
        // 如果是对象，要使用this.$set方法
        // this.$set('typeObj', 'name', e.target.value)
      }
    },

    initEditData() {
      this.positionList = this.allPositionList

      if (this.isCreate !== 1) return //编辑
      const { dutiesId, location, report, startTime, time, /*sort,*/ id } = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.dutiesId = dutiesId
      this.pram.location = location
      this.pram.report = report
      this.pram.startTime = startTime
      this.pram.time = time
      //this.pram.sort = sort
      this.pram.id = id
    },
    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      reportApi.doAdd(this.pram).then(data => {
        this.$message.success('创建工作汇报成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      reportApi.doAdd(this.pram).then(data => {
        this.$message.success('更新工作汇报成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped>

</style>
